import 'package:device_info/device_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:loan_market/confit/network.dart';
import 'package:loan_market/confit/screen_adapter.dart';
import 'package:loan_market/pages/tabs/Loans.dart';
import 'package:loan_market/pages/tabs/Me.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {

  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(icon: Image.asset('images/loans.png'), 
    activeIcon: Image.asset('images/loans_p.png'), 
    title: Text('LOANS')),
    BottomNavigationBarItem(icon: Image.asset('images/me.png'), 
    activeIcon: Image.asset('images/me_p.png'), 
    title: Text('ME')),
  ];

  final List tabBodies = [
    LoansPages(),
    MePages(),
  ];

  int currentIndex = 0;
  var currentPage;

  String _clientId = ''; //应用唯一id
  String _phoneType = ''; //机型
  String _osVersion = ''; //系统版本号

  @override
  void initState() {
    super.initState();
    currentPage = tabBodies[currentIndex];
    appVestActivation();
  }

  //进入应用上传此接口 统计用户活跃度
  appVestActivation() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    print('${androidInfo.androidId}');
    print('${androidInfo.device}');
    print('${androidInfo.version.release}');
    setState(() {
      _clientId = '${androidInfo.androidId}';
      _phoneType = '${androidInfo.device}';
      _osVersion = '${androidInfo.version.release}';
    });
    var res = await NetWork().post('report/appVestActivation', data: {
      'appKey': NetWork.appKey,
      'clientId': _clientId,
      'phoneType': _phoneType,
      'osVersion': _osVersion,
    });
    int code = res['status'];
    if (code == 200) {
      print('okokokokokokoko');
    } else {
      print('errrrrror');
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Scaffold(
      backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
      bottomNavigationBar: BottomNavigationBar(
        //此处如果超过四个底部按钮，得设置此属性，否则会被挤
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        items: bottomTabs,
        //icon图标的大小
        iconSize: 36.0,
        //选中后的颜色
        fixedColor: Color(0xFF4565F2),
        onTap: (index){
          setState(() {
            currentIndex = index;
            currentPage = tabBodies[currentIndex];
          });
        },
      ),
      body: currentPage,
    );
  }
}